package cn.com.greatwall.flowable.service.mapstruct;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.IdentityService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.User;
import org.springframework.stereotype.Component;

import cn.com.greatwall.common.base.BaseMapper;
import cn.com.greatwall.flowable.service.dto.HistoricProcessInstanceDto;
import cn.com.greatwall.flowable.service.dto.ProcessInstanceDetailDto;
import lombok.RequiredArgsConstructor;

/**
 * @Author herw
 * @Time 2020-11-24 14:08:56
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@Component
@RequiredArgsConstructor
public class ProcessInstanceDetailMapper implements BaseMapper<HistoricProcessInstanceDto, HistoricProcessInstance>  {
    private final IdentityService identityService;
    
    @Override
    public HistoricProcessInstance toEntity(HistoricProcessInstanceDto dto) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public HistoricProcessInstanceDto toDto(HistoricProcessInstance entity) {
        // TODO Auto-generated method stub
        return null;
    }
    
    public ProcessInstanceDetailDto toDto(HistoricProcessInstance entity, ProcessInstance processInstance) {
        ProcessInstanceDetailDto dto = new ProcessInstanceDetailDto();
        dto.setId(entity.getId());
        dto.setName(entity.getName());
        dto.setBusinessKey(entity.getBusinessKey());
        dto.setStartTime(entity.getStartTime());
        dto.setEndTime(entity.getEndTime());
        dto.setDurationInMillis(entity.getDurationInMillis());
        dto.setProcessDefinitionId(entity.getProcessDefinitionId());
        dto.setProcessDefinitionKey(entity.getProcessDefinitionKey());
        dto.setProcessDefinitionName(entity.getProcessDefinitionName());
        dto.setProcessDefinitionVersion(entity.getProcessDefinitionVersion());
        dto.setStartActivityId(entity.getStartActivityId());
        dto.setStartUserId(entity.getStartUserId());
        dto.setSuperProcessInstanceId(entity.getSuperProcessInstanceId());
        dto.setTenantId(entity.getTenantId());
        dto.setStartUserName(getUserName(entity.getStartUserId()));
        dto.setReason(entity.getDeleteReason());
        if (processInstance != null) {
            dto.setSuspended(processInstance.isSuspended());
        }
        
        return dto;
    }


    @Override
    public List<HistoricProcessInstance> toEntity(List<HistoricProcessInstanceDto> dtoList) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<HistoricProcessInstanceDto> toDto(List<HistoricProcessInstance> entityList) {
        // TODO Auto-generated method stub
        return null;
    }
    
    /**
     * 获取用户名称
     * @param userId /
     * @return
     */
    private String getUserName(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
        User user = identityService.createUserQuery().userId(userId).singleResult();
        if (user != null) {
            return user.getFirstName();
        }
        return null;
    }
}
